package util;


import annotation.fields;
import annotation.table;

import java.lang.reflect.Field;

public class SqlImpl {
    public static <T> StringBuffer insertSql(T t) throws IllegalAccessException {
        StringBuffer sqlBackward = new StringBuffer(" values (");
        table table = t.getClass().getAnnotation(annotation.table.class);
        String tableName = table.value();
        StringBuffer sqlFront = new StringBuffer("insert into " + tableName + " (");
        Field[] fieldList = t.getClass().getDeclaredFields();
        int i = 0;
        int length = fieldList.length;
        for (Field field : fieldList) {
            i++;
            field.setAccessible(true);
            fields fieldAnno = field.getAnnotation(fields.class);
            String fieldValue = fieldAnno.colounmName();
            sqlFront.append(fieldValue);
            if (field.get(t) != null && field.get(t) != "") {
                sqlBackward.append("'").append(field.get(t)).append("'");
            } else sqlBackward.append("+0+");
            if (i < length) {
                sqlFront.append(",");
                sqlBackward.append(",");
            }
        }
        sqlFront.append(")");
        sqlBackward.append(")");
        StringBuffer sql = new StringBuffer();
        sql.append(sqlFront).append(sqlBackward);
        sql.append(";");
        return sql;
    }

    public static <T> StringBuffer deleteOrSelectSql(T t, String dOs) throws IllegalAccessException {
        table table = t.getClass().getAnnotation(annotation.table.class);
        String tableName = table.value();//表名
        StringBuffer sql = new StringBuffer(dOs + " from " + tableName + " where ");
        Field[] fieldList = t.getClass().getDeclaredFields();
        int i = 0;
        int length = fieldList.length;
        for (Field field : fieldList) {
            i++;
            field.setAccessible(true);
            System.out.println(field.get(t) == null);
            if (field.get(t) != null && field.get(t) != "" && field.get(t) != "null") {
                System.out.println("here");
                fields fieldAnno = field.getAnnotation(fields.class);
                String fieldValue = fieldAnno.colounmName();
                sql.append(fieldValue + " = ");
                sql.append("'").append(field.get(t)).append("'");
                if (i < length) {
                    sql.append(" and ");
                }
            }
        }
        sql.append(";");
        return sql;
    }


//    public static <T> StringBuffer updateSql(T t) throws IllegalAccessException {
//        table table = t.getClass().getAnnotation(annotation.table.class);
//        String tableName = table.value();
//        StringBuffer sqlFront = new StringBuffer("update " + tableName + " set ");
//        StringBuffer sqlBackward = new StringBuffer(" where ");
//        Field[] fieldList = t.getClass().getDeclaredFields();
//        fieldList[0].setAccessible(true);
//        sqlBackward.append(fieldList[0].getAnnotation(fields.class).colounmName()).append(" = ").append("'").append(fieldList[0].get(t)).append("'");
//
//        int i = 0;
//        int length = fieldList.length;
//        for (Field field : fieldList) {
//            i++;
//            field.setAccessible(true);
//            if (field.get(t) != null && field.get(t) != "" && field.get(t) != "null") {
//                fields fieldAnno = field.getAnnotation(fields.class);
//                String fieldValue = fieldAnno.colounmName();
//                sqlFront.append(fieldValue + " = ");
//                sqlFront.append("'").append(field.get(t)).append("'");
//                if (i < length) {
//                    sqlFront.append(" , ");
//                }
//            }
//        }
//        StringBuffer sql = sqlFront;
//        sql.append(sqlBackward).append(";");
//        return sql;
//    }

//    public  static <T> StringBuffer updateByMapSql(Class<? extends Object> cls, Map<String, Object> map) throws IllegalAccessException {
//
//
//
//
//        //StringBuffer sqlBackward=new StringBuffer(" values (");
//        //System.out.println("sqlBackward="+sqlBackward);
//        table table=t.getClass().getAnnotation(annotation.table.class);
//        //table table=cls.getAnnotation(annotation.table.class);
//        //System.out.println(table);
//        String tableName=table.value();//表名
//        //System.out.println("tableName="+tableName);
//        StringBuffer sql=new StringBuffer("update "+tableName+" set ");
//        //System.out.println("sqlFront="+sqlFront);
//        Field[] fieldList=t.getClass().getDeclaredFields();
//        //System.out.println(fieldList);
//        int i=0;
//        int length = fieldList.length;
//        //System.out.println("length="+length);
//        for (Field field : fieldList) {
//            i++;
//            field.setAccessible(true);
//            //if(field!=null||field!=""){
////                fields fieldAnno=field.getAnnotation(annotation.fields.class);
////                String fieldValue=fieldAnno.colounmName();
////                //System.out.println("fieldValue="+fieldValue);
////                sqlFront.append(fieldValue);
//            //System.out.println("i="+i);
//            System.out.println(field.get(t)==null);
//            if(field.get(t) != null&&field.get(t) != ""&&field.get(t) != "null") {
//                System.out.println("here");
//                fields fieldAnno=field.getAnnotation(annotation.fields.class);
//                String fieldValue=fieldAnno.colounmName();
//                //System.out.println("fieldValue="+fieldValue);
//                sql.append(fieldValue+" = ");
//                sql.append(field.get(t));
//                if(i<length){
//                    sql.append(" and ");
//                    //sqlBackward.append(",");
//                }
//            }
////                else sqlBackward.append("+0+");
//
//        }
//
////            System.out.println("sqlFront="+sqlFront);
////            System.out.println("sqlBackward="+sqlBackward);
//        //}
////        sqlFront.append(")");
////        sqlBackward.append(")");
////        StringBuffer sql=new StringBuffer();
////        sql.append(sqlFront).append(sqlBackward);
//        //System.out.println(sql);
//        sql.append(";");
//        return sql;
//    }


}
